package start.spring.basic.job.service;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import start.spring.basic.config.CustomUidGenerator;
import start.spring.basic.dynamic.DynamicJdbc;
import start.spring.basic.dynamic.DynamicRedisDAO;
import start.spring.basic.dynamic.SpringJdbc;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.util.PageUtil;

/**
 * ****************************************************************************
 * 业务操作层基类接口实现，提供公共的service层操作方法
 *
 * @author(作者)：xuyongyun
 * @date(创建日期)：2020年4月9日 
 * 
 * *****************************************************************************
 */
@Service
public class BaseServiceImpl implements IBaseService {

	/**
	 * 静态JdbcTemplate操作依赖，提供公共的操作方法
	 */
	@Autowired
	protected SpringJdbc dao;
	
	/**
	 * 动态JdbcTemplate操作依赖，提供公共的操作方法
	 */
	@Autowired
	protected DynamicJdbc dynamicJdbc;

    /**
     * redis操作依赖，提供公共的redis操作方法
     */
    @Autowired
    protected DynamicRedisDAO redis;

    @Autowired
    protected CustomUidGenerator idGenerator;

    @Override
    public <T> IPage<T> getPage(PageQuery pageQuery) {
        return new Page<T>(pageQuery.getCurrent(), pageQuery.getSize());
    }

    @Override
    public <T> PageResult<T> getPageResult(IPage<T> page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrent(page.getCurrent());
        pageResult.setPages(page.getPages());
        pageResult.setSize(page.getSize());
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());

        return pageResult;
    }

    @Override
    public <T> PageUtil<T> getPageUtil(PageQuery pageQuery) {
        PageUtil<T> pUtil = new PageUtil<>();
        if (pageQuery != null) {
            pUtil.setCurrentPage(pageQuery.getCurrent());
            pUtil.setNumPerPage(pageQuery.getSize());
        }
        return pUtil;
    }

    @Override
    public <T> PageResult<T> getPageResult(PageUtil<T> page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrent(page.getCurrentPage());
        pageResult.setPages(page.getTotalPages());
        pageResult.setSize(page.getNumPerPage());
        pageResult.setTotal(page.getTotalRecords());
        pageResult.setRecords(page.getResultList());
        return pageResult;
    }

    @Override
    public DynamicRedisDAO getRedis() {
        return redis;
    }

    @Override
    public DataSource getDs(String dsKey) {
        return dynamicJdbc.getDs(dsKey);
    }

	@Override
	public SpringJdbc getDao() {
		return dao;
	}
}
